using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ProtoBuf;

namespace Paradogs2.api
{
    public class NetMgr {
    <#list handlerInfos as handler>
        /** ===================== ${handler.handlerName} ===================== */
        public class ${handler.handlerName}
        {
        <#list handler.apiInfos as api>
            <#if api.returnType != "void">
                public static async Task<${api.returnType}> ${api.apiName}(<#list api.parameters as param>${toCSType(param.type)} ${param.name}<#if param_has_next>, </#if></#list>)
                {
                <#if api.parameters?has_content>
                    var cg = new ${api.parameterObjectName?cap_first}();
                    <#list api.parameters as param>
                        cg.${param.name} = ${param.name};
                    </#list>
                    return await WSGameServerConnection.Request<${api.returnType}>("${api.route}", cg);
                <#else>
                    return await WSGameServerConnection.Request<${api.returnType}>("${api.route}", null);
                </#if>
                }
                <#else>
                public static void ${api.apiName}(<#list api.parameters as param>${toCSType(param.type)} ${param.name}<#if param_has_next>, </#if></#list>)
                {
                <#if api.parameters?has_content>
                    var cg = new ${api.parameterObjectName?cap_first}();
                    <#list api.parameters as param>
                        cg.${param.name} = ${param.name};
                    </#list>
                    WSGameServerConnection.Send("${api.route}", cg);
                <#else>
                    WSGameServerConnection.Send("${api.route}", null);
                </#if>
                }
            </#if>

        </#list>
        }

    </#list>
    }

<#list protobufInfos as api>
    <#if api.parameters?has_content>
    // [ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
    [ProtoContract]
    public class ${api.className?cap_first} <#if api.superClassName?has_content>: ${api.superClassName}</#if>
    {
    <#list api.parameters as param>
        [ProtoMember(${param_index + 1 + api.superClassFieldLength})]
        public ${toCSType(param.type)} ${param.name};
    </#list>
    }
    </#if>

</#list>

}

<#-- Freemarker function for Java to C# type conversion -->
<#function toCSType javaType>
    <#switch javaType>
        <#case "java.lang.String">
        <#case "String">
            <#return "string">
        <#case "java.lang.Integer">
        <#case "Integer">
            <#return "int">
        <#case "java.lang.Boolean">
        <#case "Boolean">
            <#return "bool">
        <#case "java.lang.Long">
        <#case "Long">
            <#return "long">
        <#case "java.lang.Double">
        <#case "Double">
            <#return "double">
        <#case "java.lang.Float">
        <#case "Float">
            <#return "float">
        <#case "[B">
            <#return "byte[]">
        <#case "java.time.LocalDateTime">
            <#return "DateTime">
        <#case "java.time.LocalDate">
            <#return "DateTime">
        <#default>
            <#return javaType>
    </#switch>
</#function>